<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

	<head>
		<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
		<title></title>
		<link rel="stylesheet" href="{{__PUBLIC__}}/Admin/Bootstrap/Css/bootstrap.min.css" />

		<!-- 添加upload插件 -->
		<script type="text/javascript" src="Public/uploadify/jquery-1.8.2.min.js"></script>
		<script type="text/javascript" src="Public/uploadify/jquery.uploadify.min.js"></script>
		<link rel="stylesheet" type="text/css" href="Public/uploadify/uploadify.css">

		<!-- 添加百度编辑器 -->
		<script type="text/javascript" charset="utf-8" src="{{__PUBLIC__}}/ueditor1_4_3/ueditor.config.js"></script>
		<script type="text/javascript" charset="utf-8" src="{{__PUBLIC__}}/ueditor1_4_3/ueditor.all.min.js">
		</script>
		<script type="text/javascript" charset="utf-8" src="{{__PUBLIC__}}/ueditor1_4_3/lang/zh-cn/zh-cn.js"></script>

		<!-- 气泡弹窗 -->
		<link rel="stylesheet" type="text/css" href="{{__PUBLIC__}}/Home/js/hdjs.css" />
		<script src="{{__PUBLIC__}}/Home/js/hdjs.min.js" type="text/javascript" charset="utf-8"></script>

	</head>

	<body>
		<div class="container-fluid">
			<div class="row-fluid">
				<div class="span12">

					<form action="" method='post' onsubmit="return hd_submit(this,'{{U('Goods/edit')}}','{{U('Goods/index')}}')">
						<fieldset>
							<legend>编辑 {{$goodsData['gname']}} 商品</legend>
							<table class='table table-bordered table-hover'>
								<thead>
									<tr>
										<th colspan="2" class="btn btn-primary">基本信息</th>
									</tr>
								</thead>
								<tbody>
									<tr class="info">
										<td>所属分类</td>
										<td>
											<!-- 异步显示商品属性 -->
											<script type="text/javascript">
												$(function() {
													// 获取原value
													var x = $('select[name=category_cid]').val();
													// 改变时
													$('select[name=category_cid]').change(function() {
														var pid = $(':selected').attr('pid');
														// 判断顶级分类不能添加商品
														if (pid == 0) {
															alert('顶级分类不能添加商品');
															$(this).val(x);
															return;
														}
														var tid = $(':selected', this).attr('tid');
														$.ajax({
															url: "{{U('ajaxAttr')}}",
															type: 'POST',
															dataType: 'json',
															data: {tid: tid},
															// 成功后执行回调
															success: function(attrData) {
																// 声明空字符串
																var attr = '';
																var spec = '';
																$.each(attrData, function(k, v) {
																	// 判断规格  属性
																	if (v.class == 1) {
																		// 属性
																		attr += "<tr class='info'><td>" + v.taname + "</td><td><select name='attr[" + v.taid + "]'><option value='0'>-请选择-</option>";
																		$.each(v.tavalue, function(kk, vv) {
																			attr += "<option value='" + vv + "'>" + vv + "</option>"
																		});
																		attr += "</secelt></td></tr>";
																	} else {
																		// 规格
																		spec += "<tr class='info'><td>" + v.taname + "</td><td><select name='spec[" + v.taid + "][gavalue][]'><option value='0'>-请选择-</option>";
																		$.each(v.tavalue, function(kk, vv) {
																			spec += "<option value='" + vv + "'>" + vv + "</option>"
																		});
																		spec += "</secelt></td><td>附加价格 <input type='text' name='spec[" + v.taid + "][addprice][]'/></td><td><span class='add-spec btn btn-success'><i class='icon-plus icon-white'></i>添加规格</span></td></tr>";
																	}
																});
																// 插入html
																$('#attr').html(attr);
																$('#spec').html(spec);
																$('#input_hide').html("<input type='hidden' name='type_tid' value='" + tid + "'/>");
															}
														})
													})
												})
											</script>

											<select name='category_cid'>
												<option value='0'>-请选择-</option>
												{{foreach from="$cateData" key="$ck" value="$cv"}}
												<option value='{{$cv["cid"]}}' pid='{{$cv["pid"]}}' tid='{{$cv["type_tid"]}}' {{if value='$cv["cid"]==$goodsData["category_cid"]' }} selected="selected" {{endif}}>{{$cv['_name']}}</option>
												{{endforeach}}
											</select>
										</td>
									</tr>
									<tr class="info">
										<td>所属品牌</td>
										<td>
											<select name='brand_bid'>
												<option value='0'>-请选择-</option>
												{{foreach from="$brandData" key="$bk" value="$bv"}}
												<option value="{{$bv['bid']}}" {{if value="$bv['bid']==$goodsData['brand_bid']" }} selected="selected" {{endif}}>{{$bv['bname']}}</option>
												{{endforeach}}
											</select>
										</td>
									</tr>
									<tr class="info">
										<td>商品名称</td>
										<td>
											<input type="text" name='gname' required="" value="{{$goodsData['gname']}}" />
										</td>
									</tr>
									<tr class="info">
										<td>货号</td>
										<td>
											<input type="text" name='gnumber' required="" value="{{$goodsData['gnumber']}}" />
										</td>
									</tr>
									<tr class="info">
										<td>单位</td>
										<td>
											<input type="text" name='unit' value="{{$goodsData['unit']}}" />
										</td>
									</tr>
									<tr class="info">
										<td>市价场</td>
										<td>
											<input type="text" name='mprice' value="{{$goodsData['mprice']}}" />
										</td>
									</tr>
									<tr class="info">
										<td>商城价</td>
										<td>
											<input type="text" name='sprice' value="{{$goodsData['sprice']}}" />
										</td>
									</tr>
									<tr class="info">
										<td>总库存</td>
										<td>
											<input type="text" name='stock' value="{{$goodsData['stock']}}" />
										</td>
									</tr>
									<tr class="info">
										<td>点击次数</td>
										<td>
											<input type="text" name='click' value="{{$goodsData['click']}}" />
										</td>
									</tr>
								</tbody>
							</table>

							<!-- 添加删除规格 -->
							<script type="text/javascript">
								//添加一个规格
								$('.add-spec').live('click', function() {
									var tr = $(this).parents('tr');
									var obj = tr.clone();
									var del = '<span class="del-spec btn btn-info"><i class="icon-white icon-minus"></i>删除规格</span>';
									obj.find('td').eq(3).find('.add-spec').remove();
									obj.find('td').eq(3).append(del);
									tr.after(obj);
								});
								//删除一个规格
								$('.del-spec').live('click', function() {
									$(this).parents('tr').remove();
								});
							</script>

							<p class="btn btn-primary">商品属性</p>
							<table class="table table-bordered table-hover" id='attr' class="info">
								{{foreach from="$typeAttr" key="$k" value="$v"}}

									{{if value="$v['class']==1"}}
										<tr class='info'>
											<td>{{$v['taname']}}</td>
											<td>
										
												<select name='attr[{{$v["taid"]}}]'>
													<option value='0'>-请选择-</option>
													{{foreach from="$v['tavalue']" key="$kk" value="$vv"}}
														<option value='{{$vv}}' {{if value="$v['gavalue']==$vv" }} selected="selected" {{endif}}>{{$vv}}</option>
													{{endforeach}}
												</secelt>
											</td>
										</tr>
									{{endif}} 
								{{endforeach}} 
							</table>


							
							<p class="btn btn-primary">商品规格</p>
							<table class="table table-bordered table-hover" id='spec'>
									{{foreach from="$typeAttr" key="$k" value="$v"}} 
									 		{{if value="$v['class']==2"}}
												<tr class='info'>
													<td class="spec_index">{{$v['taname']}}</td>
													<td>
														<select name="spec[{{$v['taid']}}][gavalue][]"]>
															<option value='0'>-请选择-</option>
															{{foreach from="$v['tavalue']" key="$kk" value="$vv"}}
															<option value='{{$vv}}' {{if value="$v['gavalue']==$vv" }} selected="selected" {{endif}}>{{$vv}}</option>
															{{endforeach}}
															</secelt>
													</td>
													<td>附加价格
														<input type='text' name="spec[{{$v['taid']}}][addprice][]" value='{{$v["addprice"]}}' />
													</td>
													<td>
														<span class='add-spec btn btn-success'>
															<i class='icon-plus icon-white'></i>添加规格
														</span>
														<span class="del-spec btn btn-info">
															<i class="icon-white icon-minus"></i>删除规格
														</span>
													</td>
												</tr>
											{{endif}} 
									{{endforeach}} 
							</table>


							<!-- 删除上传图片控制 -->
							<script type="text/javascript">
								$(function(){
									$('.close').live('click', function() {
										$(this).parents('li').remove();
									});
								})
							</script>
							
							<table class='table table-bordered'>
								<tr>
									<th colspan="3" class="btn btn-primary">列表图</th>
								</tr>
								<tr class="info">
									<td width="200">上传图片</td>
									<td>
										<div lab="uploadFile">
										    <input id="f_pic" type="file" multiple="true">
										    <script type="text/javascript">
										        $(function() {
										            $('#f_pic').uploadify({
										                'formData'     : {//POST数据
										                    '<?php echo session_name();?>' : '<?php echo session_id();?>',
										                },
										                'fileTypeDesc' : '上传文件',//上传描述
										                'fileTypeExts' : '*.jpg;*.png',
														'buttonImage': "{{__PUBLIC__}}/uploadify/btn.png",

										                'swf'      : '{{__PUBLIC__}}/uploadify/uploadify.swf',
										                'uploader' : '{{U('upload')}}',
										                'buttonText':'选择文件',
										                'fileSizeLimit' : '2000KB',
										                'uploadLimit' : 10,//上传文件数
										                'width':120,
										                'height':30,
										                'successTimeout':10,//等待服务器响应时间
										                'removeTimeout' : 0.2,//成功显示时间
										                'onUploadSuccess' : function(file, data, response) {
										                    //转为json
										                    data=$.parseJSON(data);
										                    //获得图片地址
										                    var imageUrl = data.url;
										                    var li="<li style='list-style:none;float:left;margin-left:10px;'><img src='"+imageUrl+"'style='width:100px;'/><input type='hidden' name='pic[]' value='"+data.path+"'/><a href='javascript:;' class='close'></a></li>";
										                    $("#uploadList_pic ul").prepend(li);
										                }
										            });
										        });
										    </script>
										    <div id="uploadList_pic">
										        <ul>
													{{foreach from="$goodsData['pic']" key="$gk" value="$gv"}}
														<li style='list-style:none;float:left;margin-left:10px;'><img src='{{$gv}}'style='width:100px;'/><input type='hidden' name='pic[]' value='{{$gv}}'/><a href='javascript:;' class='close'></a></li>
													{{endforeach}}
										        </ul>
										    </div>
										</div>
									</td>
									<td id='pic-list'></td>
								</tr>
							</table>

							
							<table class='table table-bordered'>
								<tr>
									<th colspan="3" class="btn btn-primary">商品图册</th>
								</tr>
								<style type="text/css">
									/*去掉uploadify上传按钮的边框*/
									
									.uploadify-button {
										background-color: transparent;
										border: none;
										padding: 0;
									}
									.close{
										display: block;
										float: right;
										width: 16px;
										height: 16px;
										background: url({{__PUBLIC__}}/uploadify/uploadify-cancel.png);
									}
								</style>
								<tr class="info">
									<td width="200">上传图片</td>
									<td>
										<div lab="uploadFile">
										    <input id="f_img" type="file" multiple="true">
										    <script type="text/javascript">
										        $(function() {
										            $('#f_img').uploadify({
										                'formData'     : {//POST数据
										                    '<?php echo session_name();?>' : '<?php echo session_id();?>',
										                },
										                'fileTypeDesc' : '上传文件',//上传描述
										                'fileTypeExts' : '*.jpg;*.png',
														'buttonImage': "{{__PUBLIC__}}/uploadify/btn.png",

										                'swf'      : '{{__PUBLIC__}}/uploadify/uploadify.swf',
										                'uploader' : '{{U('upload')}}',
										                'buttonText':'选择文件',
										                'fileSizeLimit' : '2000KB',
										                'uploadLimit' : 10,//上传文件数
										                'width':120,
										                'height':30,
										                'successTimeout':10,//等待服务器响应时间
										                'removeTimeout' : 0.2,//成功显示时间
										                'onUploadSuccess' : function(file, data, response) {
										                    //转为json
										                    data=$.parseJSON(data);
										                    //获得图片地址
										                    var imageUrl = data.url;
										                    var li="<li style='list-style:none;float:left;margin-left:10px;'><img src='"+imageUrl+"'style='width:100px;'/><input type='hidden' name='img[]' value='"+data.path+"'/><a href='javascript:;' class='close'></a></li>";
										                    $("#uploadList_img ul").prepend(li);
										                }
										            });
										        });
										    </script>
										    <div id="uploadList_img">
										        <ul>
													{{foreach from="$goodsData['img']" key="$gkg" value="$gvg"}}
														<li style='list-style:none;float:left;margin-left:10px;'><img src='{{$gvg}}'style='width:100px;'/><input type='hidden' name='img[]' value='{{$gvg}}'/><a href='javascript:;' class='close'></a></li>
													{{endforeach}}
										        </ul> 
										    </div>
										</div>
									</td>
									<td id='photo-list'></td>
								</tr>
							</table>

							<!-- 控制编辑器显隐 -->
							<script type="text/javascript">
								$(function() {
									$('.next_show').click(function() {
										$(this).next().toggle(600);
									})
								})
							</script>

							<table class='table'>
								<tr class="next_show">
									<th class="btn btn-primary">商品详细</th>
								</tr>
								<tr>
									<script id="editor_1" type="text/plain" style="width:100%;height:500px;" name="intro">{{$goodsData['intro']}}</script>
									<script>
										var ue = UE.getEditor('editor_1');
									</script>
								</tr>
							</table>

							<table class='table'>
								<tr class="next_show">
									<th class="btn btn-primary">售后服务</th>
								</tr>
								<tr>
									<script id="editor_2" type="text/plain" style="width:100%;height:500px;" name="service">{{$goodsData['service']}}</script>
									<script>
										var ue = UE.getEditor('editor_2');
									</script>
								</tr>
							</table>
							<table id="input_hide">
								<input type='hidden' name='type_tid' value="{{$goodsData['type_tid']}}"/>
							</table>
							<input type="hidden" name="gid" value="{{$goodsData['gid']}}" />
							<input type="submit" value="确认编辑" class="btn btn-primary btn-block btn-large" />
						</fieldset>
					</form>

				</div>
			</div>
		</div>
	</body>

</html>